/* api */
import { DeleteBaoGPTQuestionModel, GetBaoGPTQuestionListModel } from '@model/param/in/AI';
/* hooks */
import { useLoading } from '@hooks/useLoading'
import { useSettingListSearch } from '@src/modules/setting/gpt/hooks/search';
/* model */
import MsgModel from '@model/MsgModel'
import {
  SettingGPTManageTypeEnum,
  SettingGPTQuestionTypeEnum, SettingGPTQuestionTypeValueEnum
} from '@src/modules/setting/gpt/model';
/* hooks */
import { 
  useFetchSettingGPTLogList, 
  useFetchSettingGPTQuestionDelete, 
  useFetchSettingGPTQuestionList 
} from '@src/modules/setting/gpt/hooks/question';
/* util */
import { message } from '@src/util/message'
import platform from '@src/util/platform';
import { isEmpty, isFalsy } from '@src/util/type';
import { debounce } from 'pub-bbx-utils';
/* vue */
import { computed, Ref, ref, watch } from 'vue'
/* types */
import {
  SettingGPTQuestionItemType
} from '@src/modules/setting/gpt/types';

/** 
 * @description 小宝 GPT 问题列表
*/
function useSettingGPTManageList() {
  
  // 搜索参数
  const { searchParams, setPageNum, setPageSize, resetPage, setKeyword } = useSettingListSearch()
  // 选项卡
  const { setActive, setLogActive, active, isStandActive, isQuestionListActive, isLogActive } = useSettingGPTManageTab()
  
  // 删除
  const {
    loading: fetchQuestionDeleteLoading,
    isSuccess: fetchQuestionDeleteIsSuccess,
    fetchQuestionDelete
  } = useFetchSettingGPTQuestionDelete()
  
  // 获取 问题列表
  const { 
    loading: fetchQuestionListLoading,
    questionPage,
    questionList,
    fetchQuestionList
  } = useFetchSettingGPTQuestionList()
  
  // 日志列表
  const {
    loading: fetchQuestionLogListLoading,
    logPage,
    logList,
    fetchQuestionList: fetchQuestionLogList
  } = useFetchSettingGPTLogList()
  
  // 加载状态
  const loading = computed(() => {
    return (
      fetchQuestionListLoading.value
      || fetchQuestionDeleteLoading.value
      || fetchQuestionLogListLoading.value
    )
  })
  
  const fetchList = () => {
    
    // 日志
    if (isLogActive.value) {
      return fetchQuestionLogList({
        ...searchParams.value
      })
    }
    
    // 标准问题库
    if (isStandActive.value) {
      return fetchQuestionList({
        ...searchParams.value,
        type: SettingGPTQuestionTypeValueEnum[SettingGPTQuestionTypeEnum.Standard]
      })
    }
    
  }
  
  const keyword = computed(() => searchParams.value.keyword)
  
  const dataList = computed(() => {
    return isLogActive.value ? logList.value : questionList.value
  })
  
  const page = computed(() => {
    return isLogActive.value ? logPage.value : questionPage.value
  })
  
  // 初始化
  const initialize = () => {
    resetPage()
    fetchList()
  }
  
  const onSetPageNumHandler = (pageNum: number) => {
    setPageNum(pageNum)
    fetchList()
  }
  
  const onSetPageSizeHandler = (pageSize: number) => {
    setPageSize(pageSize)
    fetchList()
  }
  
  const onTabChangeHandler = (tabValue: SettingGPTManageTypeEnum) => {
    setActive(tabValue)
    resetPage()
    fetchList()
  }
  
  const debounceFetchList = debounce(fetchList, 300)
  
  const onKeywordChangeHandler = (keyword: string) => {
    setKeyword(keyword)
    debounceFetchList()
  }
  
  const onDeleteQuestionHandler = async (row: SettingGPTQuestionItemType) => {
    
    const confirmed = await platform.confirm('确定删除吗？')
    
    if (isFalsy(confirmed)) {
      return
    }
    
    const id = row.id || ''
    
    const params: DeleteBaoGPTQuestionModel = {
      ids: [id]
    }
    
    fetchQuestionDelete(params).then(() => {
      if (fetchQuestionDeleteIsSuccess.value) {
        fetchList()
      }
    })
    
  }
  
  const onDeleteBatchQuestionHandler = async (rows: SettingGPTQuestionItemType[]) => {
    
    const ids = rows.map(row => row.id || '')
    
    if (isEmpty(ids)) {
      platform.alert('请选择需要删除的数据')
      return
    }
    
    const confirmed = await platform.confirm('确定删除选择的数据吗？')
    
    if (isFalsy(confirmed)) {
      return
    }
    
    const params: DeleteBaoGPTQuestionModel = {
      ids
    }
    
    return (
      fetchQuestionDelete(params).then(async () => {
        if (fetchQuestionDeleteIsSuccess.value) {
          await fetchList()
          resetPage()
        }
      })
    )
    
  }
  
  return {
    active,
    setActive,
    setLogActive,
    isStandActive,
    isQuestionListActive,
    isLogActive,
    onTabChangeHandler,
    loading,
    initialize,
    fetchList,
    page,
    dataList,
    onSetPageNumHandler,
    onSetPageSizeHandler,
    onKeywordChangeHandler,
    keyword,
    setKeyword,
    onDeleteQuestionHandler,
    onDeleteBatchQuestionHandler
  }
}

/** 
 * @description 模型管理 选项卡
*/
function useSettingGPTManageTab() {
  
  const active: Ref<SettingGPTManageTypeEnum> = ref(SettingGPTManageTypeEnum.Standard)
  
  const setActive = (type: SettingGPTManageTypeEnum) => {
    active.value = type
  }
  
  const isActive = (type: SettingGPTManageTypeEnum) => {
    return active.value === type
  }
  
  const isStandActive = computed(() => {
    return isActive(SettingGPTManageTypeEnum.Standard)
  })
  
  const isQuestionListActive = computed(() => {
    return isActive(SettingGPTManageTypeEnum.QuestionList)
  })
  
  const isLogActive = computed(() => {
    return isActive(SettingGPTManageTypeEnum.Log)
  })
  
  const resetActive = () => {
    setActive(SettingGPTManageTypeEnum.Standard)
  }
  
  const setLogActive = () => {
    setActive(SettingGPTManageTypeEnum.Log)
  }
  
  return {
    active,
    setActive,
    resetActive,
    setLogActive,
    isLogActive,
    isStandActive,
    isQuestionListActive
  }
}

export {
  useSettingGPTManageList
}